package com.guoyh.bilui.title

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.guoyh.baseutils.ext.gone
import com.guoyh.baseutils.ext.inVisible
import com.guoyh.baseutils.ext.visible
import com.guoyh.bilui.R
import com.guoyh.baseutils.ext.clickNoRepeat
import com.guoyh.baseutils.ext.getColorm
import com.guoyh.baseutils.ext.setColors
import com.xuexiang.xui.utils.ResUtils

/**
 * Created by GUO_YH on 2020/10/24 21:49
 */
class STitleBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : LinearLayout(context, attrs, defStyle) {

    private var mLeftButtonClickListener: OnLeftButtonClickListener? = null
    private var mRightButtonClickListener: OnRightButtonClickListener? = null
    private lateinit var mViewHolder: MyViewHolder
    private var leftIcon: Drawable?
    private var rightIcon: Drawable?
    private var centerTitle: String?
    private var centerTitleColor: Int
    private var rightTitle: String?
    private var rightTitleColor: Int
    private var leftIconIsBlack = true
    private var isShowLeft = true
    private var isShowRight = false
    private var titleGravity = 0//0中间 1 左边


    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.STitleBar)
        leftIcon = typedArray.getDrawable(R.styleable.STitleBar_leftIcon)

        titleGravity = typedArray.getInt(R.styleable.STitleBar_titleGravity, 0)

        leftIconIsBlack =
            typedArray.getBoolean(R.styleable.STitleBar_leftIconIsBlack, true)
        centerTitle = typedArray.getString(R.styleable.STitleBar_sTitle)
        centerTitleColor = typedArray.getColor(
            R.styleable.STitleBar_sTitleColor,
            getColorm("#333333")
        )

        rightTitle = typedArray.getString(R.styleable.STitleBar_rightTitle)
        rightIcon = typedArray.getDrawable(R.styleable.STitleBar_rightIcon)
        rightTitleColor = typedArray.getColor(
            R.styleable.STitleBar_rightTitleColor,
            getColorm("#999999")
        )

        isShowRight = typedArray.getBoolean(R.styleable.STitleBar_isShowRight, false)
        isShowLeft = typedArray.getBoolean(R.styleable.STitleBar_isShowLeft, true)
        typedArray.recycle()
        initView()
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.libui_title_layout, this, true)

        mViewHolder = MyViewHolder(this)

        mViewHolder.tvCenterTitle.text = centerTitle
        mViewHolder.tvCenterTitle.setTextColor(centerTitleColor)

        if (titleGravity == 0) {
            mViewHolder.titleCenterGroup.gravity = Gravity.CENTER
        } else {
            mViewHolder.titleCenterGroup.gravity = Gravity.LEFT
        }


        if (leftIcon != null) {
            mViewHolder!!.titleLeftImg.setImageDrawable(leftIcon)
        } else {
            mViewHolder!!.titleLeftImg.setImageDrawable(ResUtils.getDrawable(R.drawable.icon_back_black))

            if (!leftIconIsBlack) {
                mViewHolder.titleLeftImg.setColors("#ffffff")
            }
        }


        if (isShowRight) {
            if (!rightTitle.isNullOrEmpty()) {
                mViewHolder.tvRightTitle.visible()
                mViewHolder.tvRightTitle.text = rightTitle
                mViewHolder.tvRightTitle.setTextColor(rightTitleColor)
            } else {
                mViewHolder.tvRightTitle.gone()
            }

            if (rightIcon != null) {
                mViewHolder.titleRightImg.visible()
                mViewHolder.titleRightImg.setImageDrawable(rightIcon)
            } else {
                mViewHolder.titleRightImg.gone()
            }

            mViewHolder.titleRightLayout.visible()
        } else {
            mViewHolder.titleRightLayout.inVisible()
        }

        if (isShowLeft) {
            mViewHolder.titleLeftLayout.visible()
        } else {
            mViewHolder.titleLeftLayout.inVisible()

        }

        mViewHolder.titleLeftLayout.clickNoRepeat {
            if (mLeftButtonClickListener != null) {
                mLeftButtonClickListener!!.onLeftButtonClick(it)
            }
        }

        mViewHolder!!.titleRightLayout.clickNoRepeat {
            if (mRightButtonClickListener != null) {
                mRightButtonClickListener!!.OnRightButtonClick(it)
            }
        }


    }


    /**
     * 设置标题
     *
     * @param title
     */
    fun setTitle(title: String?) {
        if (!TextUtils.isEmpty(title)) {
            mViewHolder!!.tvCenterTitle.text = title
        }
    }

    fun setTitleColor(color: Int) {
        mViewHolder!!.tvCenterTitle.setTextColor(color)

    }

    fun setLeftIconBalck() {
        if (!leftIconIsBlack) {
            leftIconIsBlack = true
            mViewHolder.titleLeftImg.setColors("#000000")
        }

    }

    fun setLeftIconWhite() {
        if (leftIconIsBlack) {
            leftIconIsBlack = false
            mViewHolder.titleLeftImg.setColors("#ffffff")
        }
    }

    fun setRightTitle(text: String?) {
        if (!TextUtils.isEmpty(text)) {
            mViewHolder?.tvRightTitle.text = text
        }
    }

    fun setRightTitleColor(textColor: Int) {
        mViewHolder?.tvRightTitle.setTextColor(textColor)
    }


    fun setIsShowRight(isShow: Boolean) {
        this.isShowRight = isShow
        mViewHolder!!.titleRightImg.visibility = if (isShowRight) View.VISIBLE else View.GONE

    }


    fun setOnLeftButtonClickListener(listen: OnLeftButtonClickListener) {
        mLeftButtonClickListener = listen
    }

    fun setOnRightButtonClickListener(listen: OnRightButtonClickListener) {
        mRightButtonClickListener = listen
    }

    interface OnLeftButtonClickListener {
        fun onLeftButtonClick(v: View)
    }

    interface OnRightButtonClickListener {
        fun OnRightButtonClick(v: View)
    }

    private inner class MyViewHolder(v: View) {
        var titleLeftLayout: FrameLayout = v.findViewById<FrameLayout>(R.id.titleLeft)
        var titleLeftImg: ImageView = v.findViewById<ImageView>(R.id.titleLeftImg)
        var tvCenterTitle: TextView = v.findViewById<TextView>(R.id.titleCenterTitle)
        var tvRightTitle: TextView = v.findViewById<TextView>(R.id.titleRight_title)
        var titleRightLayout: LinearLayout = v.findViewById<LinearLayout>(R.id.titleRight)
        var titleCenterGroup: LinearLayout = v.findViewById<LinearLayout>(R.id.titleCenterGroup)
        var titleRightImg: ImageView = v.findViewById<ImageView>(R.id.titleRight_img)

    }

}

